package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import entity.tArea;
import entity.tUser;

import service.tAreaService;
import service.tUserService;

public class AddtUserAction {
	
	private String userName;
	private String trueName;
	private String company;
	private Integer grade;
	private String email;
	private String mobilephone;
	private String phone;
	private String deviceArea;
	
	private String result;
	//private tUser tuser;
	
	private tUserService tuserService;
	 private tAreaService tareaService;
	public String add() throws IOException, ParseException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		
		List<tUser> user = new ArrayList<tUser>();
		List<tArea> lis = new ArrayList<tArea>();
		user = tuserService.searchAlltUser();
		for (tUser tuser : user){
			String username = tuser.getUserName();
			String emails = tuser.getEmail();
			
			if (userName.equals(username)){
				
				result = "用户名重复，请重新输入";
				response.setContentType("text/xml;charset=utf-8"); //更改字符编码
				System.out.println(result);
				return "error";
			}
			
			if (emails.equals(email)){
				result = "邮箱地址重复，请重新输入";
				response.setContentType("text/xml;charset=utf-8"); //更改字符编码
				System.out.println(result);
				return "error";
			}
		}
		
		if (userName.equals("")){
			result = "用户名为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (phone.equals("")){
			result = "手机号码为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			request.setAttribute("result", result);
			return "error";
		}
		
		if (mobilephone.equals("")){
			result = "电话号码为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		System.out.println(deviceArea);
		
		lis = tareaService.searchByName(deviceArea);
		
		String tAreaId = lis.get(0).getId();
		
		System.out.println(tAreaId);
		

		
		tUser tuser = new tUser();
		
		Date date=new Date();
		Timestamp ts = new Timestamp(date.getTime());
		
		//tuser.setUserId("23");
		tuser.setUserName(userName);
		System.out.println("用户名");
		System.out.println(userName);
		tuser.setTrueName(trueName);
		tuser.setEmail(email);
		tuser.setPhone(phone);
		tuser.setMobilePhone(mobilephone);
		tuser.settAreaId(tAreaId);
		
		tuser.setGrade(grade);
		tuser.setAddTime(ts);
		
		tuser.setPwd("123456");
		tuser.setWeixin(null);

		tuserService.add(tuser);
		System.out.println("添加成功！");
		
		return "success";
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getMobilephone() {
		return mobilephone;
	}

	public void setMobilephone(String mobilephone) {
		this.mobilephone = mobilephone;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getDeviceArea() {
		return deviceArea;
	}

	public void setDeviceArea(String deviceArea) {
		this.deviceArea = deviceArea;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}
	

	
	
	

}
